// 1. typeof
function printID(id: number | string) {
  if(typeof id == "string") {
    console.log(id.length);
  }
}

// 2. 平等缩小（使用==、===、!==、!=）
type Direction = 'left' | 'right' | 'up' | 'down'

function trunDirection(direction: Direction) {
  if(direction == 'left') {
    console.log('左转')
  } else if(direction == 'right') {
    console.log('右转')
  }
}

// 3. instanceof
function printDate(date: string | Date) {
  if(date instanceof Date) {
    console.log(date)
  }
}

// 4. in
interface ISwim {
  swim: () => void
}
interface IRun {
  run: () => void
}
const fish: ISwim = {
  swim: function() {}
}
const dog: IRun = {
  run: function() {}
}
function move(animal: ISwim | IRun) {
  if('swim' in animal) {
    animal.swim()
  } else if('run' in animal) {
    animal.run()
  }
}
move(fish)